import { contextBridge, ipcRenderer } from 'electron';

// 定义暴露给渲染进程的API
const electronAPI = {
  // 窗口控制
  window: {
    minimize: () => ipcRenderer.invoke('window-minimize'),
    maximize: () => ipcRenderer.invoke('window-maximize'),
    close: () => ipcRenderer.invoke('window-close'),
    isMaximized: () => ipcRenderer.invoke('window-is-maximized'),
    getState: () => ipcRenderer.invoke('window-get-state'),
    setState: (state: any) => ipcRenderer.invoke('window-set-state', state),
    onMaximized: (callback: () => void) => {
      ipcRenderer.on('window-maximized', callback);
    },
    onUnmaximized: (callback: () => void) => {
      ipcRenderer.on('window-unmaximized', callback);
    },
    onFocused: (callback: () => void) => {
      ipcRenderer.on('window-focused', callback);
    },
    onBlurred: (callback: () => void) => {
      ipcRenderer.on('window-blurred', callback);
    },
  },

  // 应用信息
  app: {
    getVersion: () => ipcRenderer.invoke('app-get-version'),
    getPlatform: () => ipcRenderer.invoke('app-get-platform'),
    getArch: () => ipcRenderer.invoke('app-get-arch'),
    quit: () => ipcRenderer.invoke('app-quit'),
    relaunch: () => ipcRenderer.invoke('app-relaunch'),
    getPath: (name: string) => ipcRenderer.invoke('app-get-path', name),
    showItemInFolder: (fullPath: string) =>
      ipcRenderer.invoke('app-show-item-in-folder', fullPath),
  },

  // 菜单控制
  menu: {
    setMenu: (template: any) => ipcRenderer.invoke('menu-set-menu', template),
    popup: (template: any) => ipcRenderer.invoke('menu-popup', template),
  },

  // 页面导航
  nav: {
    navigate: (path: string) => ipcRenderer.invoke('nav-navigate', path),
    getCurrent: () => ipcRenderer.invoke('nav-get-current'),
  },

  // 主题相关
  theme: {
    getCurrent: () => ipcRenderer.invoke('theme-get-current'),
    set: (theme: string) => ipcRenderer.invoke('theme-set', theme),
    getAvailable: () => ipcRenderer.invoke('theme-get-available'),
  },

  // 配置相关
  config: {
    get: (key: string) => ipcRenderer.invoke('config-get', key),
    set: (key: string, value: any) => ipcRenderer.invoke('config-set', key, value),
    delete: (key: string) => ipcRenderer.invoke('config-delete', key),
    getAll: () => ipcRenderer.invoke('config-get-all'),
  },

  // 通知相关
  notification: {
    show: (options: any) => ipcRenderer.invoke('notification-show', options),
    requestPermission: () => ipcRenderer.invoke('notification-request-permission'),
  },

  // 对话框相关
  dialog: {
    showOpen: (options: any) => ipcRenderer.invoke('dialog-show-open', options),
    showSave: (options: any) => ipcRenderer.invoke('dialog-show-save', options),
    showMessage: (options: any) => ipcRenderer.invoke('dialog-show-message', options),
    showError: (options: any) => ipcRenderer.invoke('dialog-show-error', options),
  },

  // 事件监听器管理
  removeAllListeners: (channel: string) => {
    ipcRenderer.removeAllListeners(channel);
  },
};

// 将API暴露给渲染进程
contextBridge.exposeInMainWorld('electronAPI', electronAPI);

// 类型声明，供TypeScript使用
export type ElectronAPI = typeof electronAPI;

// 声明全局类型
declare global {
  interface Window {
    electronAPI: ElectronAPI;
  }
}